<template>
  <div>
    <div class="header">
      <span style="margin-left: 10px">关键字</span>
      <el-input
        placeholder="请输入关键字"
        v-model="req.keyword"
        style="width: 200px; margin-left: 10px"
      ></el-input>
      <span style="margin-left: 10px">月份</span>
      <el-date-picker
        type="month"
        v-model="req.month"
        placeholder="请选择月份"
        value-format="yyyy-MM-dd HH:mm:ss"
        style="width: 200px; margin-left: 10px"
      ></el-date-picker>
      <el-button
        type="primary"
        style="margin-left: 10px"
        size="small"
        v-if="req.month"
        @click="getInworkerIncomes"
      >
        查询提成
      </el-button>
      <el-button
        type="warning"
        style="margin-left: 10px"
        size="small"
        v-if="req.month"
        @click="createInworkerIncomes"
      >
        生成{{ req.month | formatMonth }}提成
      </el-button>
      <el-button
        type="success"
        style="margin-left: 10px"
        size="small"
        @click="outputExcelVis = true"
      >
        导出提成
      </el-button>
    </div>
    <div class="main">
      <el-table
        style="overflow: auto"
        :data="res"
        stripe
        border
        highlight-current-row
        show-summary
      >
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column label="提成所属" width="200">
          <template slot-scope="scope">
            {{ scope.row.USR_Name }}【{{ scope.row.USR_Phone }}】
          </template>
        </el-table-column>
        <el-table-column label="案件">
          <template slot-scope="scope">
            <el-link
              v-if="scope.row.CS_No"
              type="primary"
              @click="toCase(scope.row.CEI_CaseID)"
            >
              流水号：{{ scope.row.CS_No }}
            </el-link>
            <div v-if="scope.row.CS_DisplayName">
              案件名：{{ scope.row.CS_DisplayName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="开票金额" width="80px">
          <template slot-scope="scope">{{ scope.row.CEI_BillSum }}元</template>
        </el-table-column>
        <el-table-column label="提成基数" width="80px">
          <template slot-scope="scope">
            <el-link type="primary" @click="editBaseSum(scope.row)">
              {{ scope.row.CEI_BaseSum }}元
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="贡献率" width="80px">
          <template slot-scope="scope">
            {{ scope.row.CEI_ContributionRate }}%
          </template>
        </el-table-column>
        <el-table-column label="提成" prop="CEI_Fee">
          <template slot-scope="scope">{{ scope.row.CEI_Fee }}元</template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="导出提成"
        :visible.sync="outputExcelVis"
        width="400px"
        :modal-append-to-body="false"
        center
      >
        <el-form label-width="100px" style="margin-top: 20px; width: 80%">
          <el-form-item label="关键字">
            <el-input
              placeholder="请输入关键字"
              v-model="excelReq.keyword"
              style="width: 200px; margin-left: 10px"
            ></el-input>
          </el-form-item>
          <el-form-item label="起始月份">
            <el-date-picker
              type="month"
              v-model="excelReq.month1"
              placeholder="请选择起始月份"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 200px; margin-left: 10px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="截止月份">
            <el-date-picker
              type="month"
              v-model="excelReq.month2"
              placeholder="请选择截止月份"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 200px; margin-left: 10px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="margin-left: 10px"
              size="small"
              @click="outputExcel"
            >
              确定
            </el-button>
            <el-button
              type="text"
              style="margin-left: 10px"
              size="small"
              @click="outputExcelVis = false"
            >
              取消
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import companyEmployeesChoose from '@/views/web/orgnization/components/companyEmployeesChoose.vue'
import {
  getInworkerIncomes,
  createInworkerIncomes,
  editBaseSum,
} from '@/api/finance/caseEmployeeIncomeStas'
import { baseURL, tokenName, empIdName } from '@/config'
import axios from 'axios'

export default {
  components: { companyEmployeesChoose },
  data: function () {
    return {
      req: {
        month: null,
      },
      res: [],
      excelReq: {
        month1: null,
        month2: null,
        keyword: '',
      },
      outputExcelVis: false,
    }
  },
  methods: {
    editBaseSum(income) {
      this.$prompt('请输入案件' + income.CS_No + '的提成基数', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /[0-9]/,
        inputErrorMessage: '请输入数字',
      })
        .then(({ value }) => {
          editBaseSum({
            month: income.CEI_Time,
            caseId: income.CEI_CaseID,
            sum: value,
          }).then(() => {
            this.getInworkerIncomes()
          })
        })
        .catch(() => {})
    },
    getInworkerIncomes() {
      getInworkerIncomes(this.req).then(({ data }) => {
        this.res = data
      })
    },
    toCase: function (caseId) {
      this.$router.push({
        name: 'caseInfoPage',
        query: { caseId },
      })
    },
    createInworkerIncomes(emps) {
      this.$confirm(
        `确认生成提成？（如当月员工已有提成数据，旧数据会被新数据替换）`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).then(() => {
        // this.createReq.emps = emps
        createInworkerIncomes(this.req).then(() => {
          this.$message('生成成功')
          // this.req.month = this.createReq.month
          this.getInworkerIncomes()
        })
      })
    },

    outputExcel() {
      var config = {
        baseURL,
        headers: {},
        responseType: 'blob',
      }
      config.headers[tokenName] = this.$store.getters['user/accessToken']
      config.headers[empIdName] = this.$store.getters['user/curEmp'].EMP_ID
      axios
        .post(
          '/api/Finance/CaseEmployeeIncomeStas/OutputInworkerExcel',
          this.excelReq,
          config
        )
        .then((response) => {
          var blob = new Blob([response.data])
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) //创建下载的链接
          downloadElement.href = href
          downloadElement.download = '提成报表.xls' //下载后文件名
          document.body.appendChild(downloadElement)
          downloadElement.click() //点击下载
          document.body.removeChild(downloadElement) //下载完成移除元素
          window.URL.revokeObjectURL(href) //释放掉blob对象
          this.outputExcelVis = false
        })
        .catch((error) => {
          console.log('response: ', error)
        })
    },
  },
  created: function () {
    var data = this.$store.getters['pagination/getRoutePage'](this.$route)
    Object.assign(this.$data, data)
  },
  beforeRouteLeave(to, from, next) {
    this.$store.dispatch('pagination/setRoutePage', {
      route: this.$route,
      pageData: this.$data,
    })
    next()
  },
}
</script>
<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
.main {
  height: 75vh;
  overflow: auto;
}
</style>
